<?php
namespace app\admin\controller;
use app\admin\model\TyMember;
use think\Controller;
use app\admin\model\Member as UM;
use app\admin\model\Role;
use app\admin\model\Course;
use app\admin\model\CourseCate;
use app\admin\model\Lecturer;
use app\admin\model\CourseSubject;
use app\admin\model\MemberClass;
use app\admin\model\ClassCourse;
use app\admin\validate\MemberValidate;
use think\Db;

class Member extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        //$this->doCheckAuth($this->noCheckAction);
    }
    /**
    * 列表
    */
    public function index()
    {
        
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list($where,10,'id desc');
        $count = $model->_count($where);
        
        $types = UM::Member_TYPE;
        
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
            'types'=>$types
        ]);
        
        return $this->fetch();
    }

    /**
    * 添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            //var_dump($request);die;
            $validate = new MemberValidate();
            if(!$validate->scene($validate::SCENE_ADD)->check($request)){
                die(outputError($validate->getError()));
            }
            
            $model = new UM();
            $config = $this->sysconfig;
           
            $ret = $model->_create($request, $config);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '添加成功'));
        }else{

            return $this->fetch();
        }
    }


    /*
     * 重置密码
     */
    public function czmm(){
        if(request()->isPost()){
            $id=input('id');
            $pass=createPassword('123456');
            $res=db('member')->where('id',$id)->setField('pwd',$pass);

            if($res!==false){
                die(outputSuccess([], '重置成功'));
            }else{
                die(outputError('重置失败'));
            }
        }
    }
    /**
    * 编辑
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $mid     = $request['id'];
            $up['nick_name'] = $request['nickname'];
            $ret = Db::name('member_profile')->where('mid',$mid)->update($up);
            if(!$ret){
                die(outputError('编辑失败'));
            }
            die(outputSuccess([], '编辑成功'));
        }else{
            $id = (int)$this->request->get('id');
            $data = Db::name('member')
                ->alias('m')
                ->field('m.id,m.user_name,p.nick_name')
                ->join('member_profile p','p.mid=m.id')
                ->where('m.id',$id)
                ->find();
            $this->assign(['data'=>$data]);
            return $this->fetch();
        }
    }
    
    /**
    * 删除
    */
    public function del(){
        if($this->request->isAjax()){
            $id = (int)$this->request->post('id');
            
            $validate = new MemberValidate();
            if(!$validate->scene($validate::SCENE_DEL)->check(['id'=>$id])){
                die(outputError($validate->getError()));
            }
            
            $model = new UM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    /**
    * 批量删除
    */
    public function delAll(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new UM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    
   
    
    /**
    * 设置状态
    */
    public function set_status(){
        $request = $this->request->post();
        //var_dump($request);die;
        $model = new UM();
        $ret = $model->_setStatus($request);
        if(!$ret){
            die(outputError($model->getError()));
        }
        
        die(outputSuccess());
    }
    
    
    
    /**
    * 查看设置详情
    */
    public function detail(){
        $id = (int)$this->request->get('id');
        
        $model = new UM();
        $data = $model->_findOne($id);
        $this->assign([
            'data' => $data,
        ]);
        //var_dump($data);die;
        return $this->fetch();
    }
    
    /**
    * 开通课程
    */
    public function openCourse(){
        
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new MemberClass;
            $ret = $model->_openCourse($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '开通成功'));
        }else{
            $model = new ClassCourse();
            $request = $this->request->get();
            $search_where = $model->_where($request);
            $where = $search_where['where'];
            $search = $search_where['search'];
            $data = $model->_list($where);
            $count = $model->_count($where);
            

            
            $this->assign([
                'search' => $search,
                'list' => $data['list'],
                'page' => $data['page'],
                'count' => $count,
                'mid' => (int)$request['id'],
            ]);

            return $this->fetch();
        }

    }
    
    /**
    * 设置课程有效期
    */
    public function setClassEffective(){
        $request = $this->request->get();
        $id = (int)$request['id'];
        $mid = (int)$request['mid'];
        
        $model = new ClassCourse;
        $data = $model->_findOne($id);
        
        $this->assign([
            'id' => $id,
            'mid' => $mid,
            'data' => $data,
        ]);
        return $this->fetch();
    }

    /**
     * 开通题库
     */
    public function openQuestion(){

        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new TyMember();
            $ret = $model->_openQuestion($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '开通成功'));
        }else{
            $model = new \app\admin\model\Question();
            $request = $this->request->get();
            $search_where = $model->_where($request);
            $where = $search_where['where'];
            $search = $search_where['search'];
            $data = $model->_list($where);
            $result0 = $data['list']->all();
            $uid = (int)$request['id'];
            foreach ($result0 as $k=>$v){
                $maps0['cate'] = $v['id'];
                $maps0['uid']  = $uid;
                $is_open = Db::name('tk_member')->where($maps0)->find();
                if($is_open){
                    $v['is_open']   = 1;
                }else{
                    $v['is_open']   = 0;
                }
                $data['list'][$k]    = $v;
            }
            $count = $model->_count($where);
            $this->assign([
                'search' => $search,
                'list' => $data['list'],
                'page' => $data['page'],
                'count' => $count,
                'mid' => (int)$request['id'],
            ]);

            return $this->fetch();
        }

    }

}
